/* 
*
*管控策略
*/
(function () {
    let onOff = true 
    $.ajax({
        url: window.base_url + '/ccp/light/openTime',
        type: 'get',
        dataType: 'json',
        data: {
            token: window.token
        },
        success: function (res) {
            // console.log(res)
            if(~~res.retcode == 200) {
                if(res.data) {
                    var dataArr = res.data.split('-');
                    // $('#lightstarttime').val(dataArr[0]);
                    // $('#lightendtime').val(dataArr[1]);
                    $('#setLightBtn .on-btn').css('display', 'block');
                    $('#setLightBtn .off-btn').css('display','none');
                } else {
                    $('#setLightBtn .on-btn').css('display', 'none');
                    $('#setLightBtn .off-btn').css('display', 'block');
                }
            }
        },
        error: function (err) {
            console.log(err)
        }
    })
    $('#setLightBtn').click(function(){
        $('#lightstarttime').val('00:00');
        $('#lightendtime').val('06:00');
        const startTime = $('#lightstarttime').val();
        const endTime = $('#lightendtime').val();
        $.ajax({
            url: window.base_url + '/ccp/light/openTime',
            type: 'post',
            dataType: 'json',
            data: {
                openTime: startTime+'-'+endTime,
                token: window.token
            },
            success: function (res) {
                // console.log(res)
                if(~~res.retcode == 200) {
                    if(onOff) {
                        onOff = false
                        $('#setLightBtn .on-btn').css('display', 'none');
                        $('#setLightBtn .off-btn').css('display', 'block');
                    }else{
                        onOff = true
                        $('#setLightBtn .on-btn').css('display', 'block');
                        $('#setLightBtn .off-btn').css('display', 'none');
                    }
                    
                }else{
                    $('#setLightBtn .on-btn').css('display', 'block');
                    $('#setLightBtn .off-btn').css('display', 'none');
                }
            },
            error: function (err) {
                console.log(err)
                if(onOff) {
                    onOff = false
                    $('#setLightBtn .on-btn').css('display', 'none');
                    $('#setLightBtn .off-btn').css('display', 'block');
                }else{
                    onOff = true
                    $('#setLightBtn .on-btn').css('display', 'block');
                    $('#setLightBtn .off-btn').css('display', 'none');
                }
            }
        })
    })
    
})()